package com.ruoyi.paper.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.demo.domain.vo.GoodsVo;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.mapper.SysOssMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.paper.domain.bo.ExamPaperBo;
import com.ruoyi.paper.domain.vo.ExamPaperVo;
import com.ruoyi.paper.domain.ExamPaper;
import com.ruoyi.paper.mapper.ExamPaperMapper;
import com.ruoyi.paper.service.IExamPaperService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 试卷Service业务层处理
 *
 * @author fjw
 * @date 2024-04-16
 */
@RequiredArgsConstructor
@Service
public class ExamPaperServiceImpl implements IExamPaperService {

    private final ExamPaperMapper baseMapper;
    private final SysOssMapper sysOssMapper;

    /**
     * 查询试卷
     */
    @Override
    public ExamPaperVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询试卷列表
     */
    @Override
    public TableDataInfo<ExamPaperVo> queryPageList(ExamPaperBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExamPaper> lqw = buildQueryWrapper(bo);
        Page<ExamPaperVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        List<ExamPaperVo> records = result.getRecords();
        //将里面的image属性 从sys_oss表里面的oss_id 改成url
        records.forEach(item -> {
            if (StringUtils.isNotBlank(item.getFile())) {
                SysOssVo sysOssVo = sysOssMapper.selectVoById(item.getFile());
                item.setFile(sysOssVo.getUrl());
            }
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询试卷列表
     */
    @Override
    public List<ExamPaperVo> queryList(ExamPaperBo bo) {
        LambdaQueryWrapper<ExamPaper> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ExamPaper> buildQueryWrapper(ExamPaperBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExamPaper> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, ExamPaper::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增试卷
     */
    @Override
    public Boolean insertByBo(ExamPaperBo bo) {
        ExamPaper add = BeanUtil.toBean(bo, ExamPaper.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改试卷
     */
    @Override
    public Boolean updateByBo(ExamPaperBo bo) {
        ExamPaper update = BeanUtil.toBean(bo, ExamPaper.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ExamPaper entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除试卷
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
